package com.hualan.sfzy.controller.ssh;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTest;
import com.hualan.sfzy.service.impl.PsychologyTestServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/psychologyTest")
public class PsychologyTestController {
    @Autowired
    private PsychologyTestServiceImpl Service;

    /*添加*/
    @PostMapping("/add")
    public Result add(@RequestBody PsychologyTest entity) {
        int i = Service.add(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*编辑*/
    @PutMapping("/edit")
    public Result edit(@RequestBody PsychologyTest entity) {
        int i = Service.edit(entity);
        if (i > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /*删除*/
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        int i = Service.remove(id);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*批量删除*/
    @DeleteMapping("/delBatch")
    public Result delBatch(@RequestParam("ids") Integer[] ids) {
        // 参数校验
        if (ids == null || ids.length == 0) {
            return Result.error("请提供有效的删除 ID 列表");
        }
        int i = Service.removeBatch(ids);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error("未删除任何记录");
    }

    /*查询所有*/
    @GetMapping("/find")
    public Result find() {
        List<PsychologyTest> list = Service.find();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }

    /*分页查询所有*/
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                           PsychologyTest entity) {
        Map<String, Object> map = Service.findPage(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

    /*根据id查询单个*/
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        PsychologyTest psychologyTest = Service.findById(id);
        if (psychologyTest != null) {
            return Result.success(psychologyTest);
        }
        return Result.error("未找到ID为" + id + "的记录");
    }


    /*根据id查询单个(多表)*/
    @GetMapping("/findByIdDuo/{id}")
    public Result findByIdDuo(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        PsychologyTest psychologyTest = Service.findByIdDuo(id);
        if (psychologyTest != null) {
            return Result.success(psychologyTest);
        }
        return Result.error("未找到ID为" + id + "的多表记录");
    }

    /*根据id修改(单表)*/
    @PutMapping("/editById")
    public Result editById(@RequestBody PsychologyTest entity) {
        int i = Service.editById(entity);
        if (i > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /*多表查询*/
    @GetMapping("/findDuo")
    public Result findDuo(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        Map<String, Object> map = Service.selectDuo(pageNum, pageSize);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

    /*根据title(模糊)多条件查询*/
    @GetMapping("/findByCondition")
    public Result findByCondition(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                  PsychologyTest entity) {
        Map<String, Object> map = Service.findByCondition(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }


    /*查询所有分类*/
    @GetMapping("/findAllClassify")
    public Result findAllClassify() {
        List<PsychologyTest> list = Service.selectAllClassify();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }


    /*查询所有id 和对应的 title*/
    @GetMapping("/findAllIdAndTitle")
    public Result findAllIdAndTitle() {
        List<PsychologyTest> list = Service.selectAllIdAndTitle();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }


    /*根据testid查询问题列表，并获取问题列表里的选项*/
    @GetMapping("/selectAllTitleAndOptions/{testId}")
    public Result selectAllTitleAndOptions(@PathVariable("testId") Integer testId) {
        List<PsychologyTest> list = Service.selectAllTitleAndOptions(testId);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error("未找到ID为" + testId + "的多表记录");
    }

}
